﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web;
using System.Data;
using System.Configuration;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
using System.Drawing;
using System.Net.Mail;

using System.Web.Services;

using System.IO.MemoryMappedFiles;

public partial class partner_partner_add_property : System.Web.UI.Page
{
    string p_id;
    private byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, System.Drawing.Imaging.ImageFormat formatOfImage)
    {
        byte[] Ret;
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                imageToConvert.Save(ms, formatOfImage);
                Ret = ms.ToArray();
            }
        }
        catch (Exception) { throw; }
        return Ret;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        txtRooms.Text = "0";
        TextBox16.Text = "0";
        txtAssetAmount.Text = "N/A";
        if (Session["email"] == null)
        {
            Response.Redirect("sessiont.aspx");
        }
        Label1.Text = Session["email"].ToString();
        txtcreate.Text = System.DateTime.Now.ToString();
        txtagent.Text = Session["email"].ToString();



    }
    //[WebMethod]




    //public static string InsertMethod(string block, string sub, string zone, string asset_amount, string b_prevlage, string dob, string g_area, string room, string b_a, string b_b, string b_c, string property, string city, string street, string price, string o_price, string color, string rooms, string premises, string age, string property_tax, string r_estimate, string additions, string basement, string garden, string level1, string strong, string ac, string balcony, string rooms2, string premises2, string age2, string prop_tax2, string r_est2, string additions2, string basement2, string garden2, string level2, string strong2, string ac2, string balcony2, string room3, string premises3, string age3, string p_tax3, string r_est3, string add3 , string base3, string level3, string strong3, string garden3, string ac3,string balcony3)
    //{

    //    SqlConnection conn = null;
    //    conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //    {
    //        SqlCommand cmd = new SqlCommand("Insert into properties values('" + block + "', '" + sub + "', '" + zone + "', '" + asset_amount + "', '" + b_prevlage + "', '" + dob + "', '" + g_area + "', '" + room + "', '" + b_a + "', '" + b_b + "', '" + b_c + "', '" + property + "', '" + city + "', '" + street + "', '" + price + "', '" + o_price + "', '" + color + "','" + rooms + "','" + premises + "','" + age + "','" + property_tax + "','" + r_estimate + "','" + additions + "','" + basement + "','" + garden + "','" + level1 + "','" + strong + "','" + ac + "','" + balcony + "','" + rooms2 + "','" + premises2 + "','" + age2 + "','" + prop_tax2 + "','" + r_est2 + "','" + additions2 + "','" + basement2 + "','" + garden2 + "','" + level2 + "','" + strong2 + "','" + ac2 + "','" + balcony + "','" + room3 + "','" + premises3 + "','" + age3 + "','" + p_tax3 + "','" + r_est3 + "','" + add3 + "','" + base3 + "','" + level3 + "','" + strong3 + "','" + garden3 + "','" + ac3 + "','" + balcony3 + "')", conn);
    //        {
    //            conn.Open();
    //            cmd.ExecuteNonQuery();
    //            return "True";
    //        }
    //    }
    //}
    protected void Save_Click(object sender, System.EventArgs e)
    {

        Random r1 = new Random();
        int num = r1.Next(0, 99999999); // generate random number
        string number = string.Format("{0:D8}", num);

        Random r2 = new Random();
        int num2 = r2.Next(0, 26); // generate random letter
        char letter = (char)('A' + num2);


        p_id = letter + number; // join both as ID 
        SqlConnection conn = null;
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
        conn.Open();
        string filename = FileUpload1.PostedFile.FileName.ToString();



        System.Drawing.Image imag = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
        SqlCommand cmd = new SqlCommand("Insert into properties values(@type,@p_id,@fname,@mname,@lname,@dob,@occupation,@houseno,@city,@street,@ssn,@zip ,@country,@prop_type,@cust_proptype,@prop_cond,@cust_prop_cond,@block,@sub,@zone,@asset_amount,@b_privelege,@g_area,@room,@elevator,@roof,@electric,@no_of_residence,@profile_pic,@parking,@custom_park,@furniture,@custom_furniture,@toilet,@custom_toilet,@shower,@custom_shower, @kitchen,@view_prop,@custom_view,@front_rear_view ,@custom_front_rear,@garden,@custom_garden, @evacuation_date,@custome_evacuation_date,@land,@custom_land,@area,@custom_area,@p_property,@p_city,@p_street,@p_price,@p_oldprice,@p_color,@p_rooms,@p_premises,@p_age,@p_prop_tax,@p_renting_estimate,@p_additions,@p_basement,@p_level,@p_strong,@p_ac,@p_balconies,@p_balcony_area,@p_balcony_no,@p_maintance,@p_building,@p_boiler,@p_rooms2,@p_premises2,@p_age2,@p_prop_tax2,@p_renting_estimate2,@p_additions2,@p_basement2,@p_level2,@p_strong2,@p_ac2,@p_balconies2,@p_balcony_area2,@p_balconie_no2,@p_maintance2,@p_building2,@p_boiler2,@p_rooms3,@p_premises3,@p_age3,@p_prop_tax3,@p_renting_estimate3,@p_additions3,@p_basement3,@p_level3,@p_strong3,@p_ac3,@p_balconies3,@p_balcony_area3,@p_balconie_no3,@p_maintance3,@p_building3,@p_boiler3,@p_north,@p_south,@p_east,@p_west,@p_smart,@p_alarm,@p_bars,@p_handi,@p_teather,@p_gym,@p_zacuji,@p_pool,@status1,@adding_date,@adding_by)", conn);

        cmd.Parameters.AddWithValue("@type", DropDownList5.SelectedValue);
        cmd.Parameters.AddWithValue("@p_id", p_id);

        cmd.Parameters.AddWithValue("@fname", txtfname1.Text);
        cmd.Parameters.AddWithValue("@mname", txtmname.Text);
        cmd.Parameters.AddWithValue("@lname", txtlname1.Text);



        cmd.Parameters.AddWithValue("@dob", txtdob1.Text);
        cmd.Parameters.AddWithValue("@occupation", txtoccupation.Text);
        cmd.Parameters.AddWithValue("@houseno", txthno.Text);



        cmd.Parameters.AddWithValue("@city", txtcity.Text);
        cmd.Parameters.AddWithValue("@street", TextBox3.Text);
        cmd.Parameters.AddWithValue("@ssn", txtssn.Text);
        cmd.Parameters.AddWithValue("@zip", txtzip.Text);
        cmd.Parameters.AddWithValue("@country", txtcountry.Text);

        cmd.Parameters.AddWithValue("@prop_type", DropDownList5.SelectedValue);




        cmd.Parameters.AddWithValue("@cust_proptype", DropDownList7.SelectedValue);
        cmd.Parameters.AddWithValue("@prop_cond", DropDownList8.SelectedValue);
        cmd.Parameters.AddWithValue("@cust_prop_cond", TextBox5.Text);


        cmd.Parameters.AddWithValue("@block", txtBlock.Text);
        cmd.Parameters.AddWithValue("@sub", txtSub.Text);
        cmd.Parameters.AddWithValue("@zone", txtZone.Text);
        cmd.Parameters.AddWithValue("@asset_amount", txtAssetAmount.Text);

        cmd.Parameters.AddWithValue("@b_privelege", txtBuildPrivilege.Text);
        cmd.Parameters.AddWithValue("@g_area", txtGeneralArea.Text);
        cmd.Parameters.AddWithValue("@room", txtRoom.Text);
        cmd.Parameters.AddWithValue("@elevator", txtBlockA.Text);

        cmd.Parameters.AddWithValue("@roof", txtBlockB.Text);

        cmd.Parameters.AddWithValue("@electric", txtBlockC.Text);
        cmd.Parameters.AddWithValue("@no_of_residence", TextBox12.Text);
        cmd.Parameters.Add("@profile_pic", SqlDbType.Image, 0).Value = ConvertImageToByteArray(imag, System.Drawing.Imaging.ImageFormat.Jpeg);

        //cmd.Parameters.AddWithValue("@p_rooms", txtStrong.SelectedValue);
        cmd.Parameters.AddWithValue("@parking", ddlparking.SelectedValue);
        cmd.Parameters.AddWithValue("@custom_park", txtpark.Text);


        cmd.Parameters.AddWithValue("@furniture", DropDownList18.SelectedValue);
        cmd.Parameters.AddWithValue("@custom_furniture", txtfurniture.Text);
        cmd.Parameters.AddWithValue("@toilet", DropDownList19.SelectedValue);
        cmd.Parameters.AddWithValue("@custom_toilet", txttoilets.Text);
        cmd.Parameters.AddWithValue("@shower", DropDownList20.SelectedValue);
        cmd.Parameters.AddWithValue("@custom_shower", TextBox16.Text);

        cmd.Parameters.AddWithValue("@kitchen", DropDownList10.SelectedValue);

        cmd.Parameters.AddWithValue("@view_prop", DropDownList11.SelectedValue);

        cmd.Parameters.AddWithValue("@custom_view", TextBox8.Text);




        cmd.Parameters.AddWithValue("@front_rear_view", DropDownList16.SelectedValue);
        cmd.Parameters.AddWithValue("@custom_front_rear", TextBox11.Text);
        cmd.Parameters.AddWithValue("@garden", DropDownList24.SelectedValue);

        cmd.Parameters.AddWithValue("@custom_garden", TextBox15.Text);
        cmd.Parameters.AddWithValue("@evacuation_date", DropDownList12.SelectedValue);
        cmd.Parameters.AddWithValue("@custome_evacuation_date", TextBox7.Text);





        cmd.Parameters.AddWithValue("@land", DropDownList15.SelectedValue);
        cmd.Parameters.AddWithValue("@custom_land", TextBox10.Text);
        cmd.Parameters.AddWithValue("@area", DropDownList25.SelectedValue);
        cmd.Parameters.AddWithValue("@custom_area", TextBox17.Text);

        cmd.Parameters.AddWithValue("@p_property", txtProperty.Text);
        cmd.Parameters.AddWithValue("@p_city", txtCity1.Text);
        cmd.Parameters.AddWithValue("@p_street", txtStreet.Text);
        cmd.Parameters.AddWithValue("@p_price", txtPrice.Text);


        cmd.Parameters.AddWithValue("@p_oldprice", txtOldPrice.Text);
        cmd.Parameters.AddWithValue("@p_color", txtColor.Text);
        cmd.Parameters.AddWithValue("@p_rooms", txtRooms.Text);
        cmd.Parameters.AddWithValue("@p_premises", txtPremises.Text);
        cmd.Parameters.AddWithValue("@p_age", txtAge.Text);
        cmd.Parameters.AddWithValue("@p_prop_tax", txtPropertyTax.Text);
        cmd.Parameters.AddWithValue("@p_renting_estimate", txtRentEstimate.Text);

        cmd.Parameters.AddWithValue("@p_additions", txtAdditions.Text);
        cmd.Parameters.AddWithValue("@p_basement", txtBasement.Text);
        cmd.Parameters.AddWithValue("@p_level", txtLevel.Text);
        cmd.Parameters.AddWithValue("@p_strong", txtStrong.Text);
        cmd.Parameters.AddWithValue("@p_ac", txtAC.Text);
        cmd.Parameters.AddWithValue("@p_balconies", txtBalconies.Text);
        cmd.Parameters.AddWithValue("@p_balcony_area", TextBox6.Text);




        cmd.Parameters.AddWithValue("@p_balcony_no", DropDownList9.SelectedValue);
        cmd.Parameters.AddWithValue("@p_maintance", TextBox9.Text);
        cmd.Parameters.AddWithValue("@p_building", DropDownList13.SelectedValue);
        cmd.Parameters.AddWithValue("@p_boiler", DropDownList14.SelectedValue);

        cmd.Parameters.Add("@p_rooms2", txtRooms2.Text);
        cmd.Parameters.AddWithValue("@p_premises2", txtPremises2.Text);
        cmd.Parameters.AddWithValue("@p_age2", txtAge2.Text);
        cmd.Parameters.AddWithValue("@p_prop_tax2", txtPropertyTax2.Text);
        cmd.Parameters.AddWithValue("@p_renting_estimate2", txtRentingEstimate2.Text);

        cmd.Parameters.AddWithValue("@p_additions2", txtAdditions2.Text);
        cmd.Parameters.AddWithValue("@p_basement2", txtBasement2.Text);
        cmd.Parameters.AddWithValue("@p_level2", txtLevel2.Text);
        cmd.Parameters.AddWithValue("@p_strong2", txtStrongRoom2.Text);
        cmd.Parameters.AddWithValue("@p_ac2", txtAC2.Text);
        cmd.Parameters.AddWithValue("@p_balconies2", txtBalcony2.Text);
        cmd.Parameters.AddWithValue("@p_balcony_area2", "0");


        cmd.Parameters.AddWithValue("@p_balconie_no2", "0");
        cmd.Parameters.AddWithValue("@p_maintance2", "0");
        cmd.Parameters.AddWithValue("@p_building2", "0");
        cmd.Parameters.AddWithValue("@p_boiler2", "0");
        cmd.Parameters.Add("@p_rooms3", txtRooms3.Text);
        cmd.Parameters.AddWithValue("@p_premises3", txtPremises3.Text);
        cmd.Parameters.AddWithValue("@p_age3", txtAge3.Text);
        cmd.Parameters.AddWithValue("@p_prop_tax3", txtPropertyTax3.Text);
        cmd.Parameters.AddWithValue("@p_renting_estimate3", txtRentingEstimate3.Text);

        cmd.Parameters.AddWithValue("@p_additions3", txtAdditions3.Text);
        cmd.Parameters.AddWithValue("@p_basement3", txtBasement3.Text);
        cmd.Parameters.AddWithValue("@p_level3", txtLevel3.Text);
        cmd.Parameters.AddWithValue("@p_strong3", txtStrongRoom3.Text);
        cmd.Parameters.AddWithValue("@p_ac3", txtAC3.Text);




        cmd.Parameters.AddWithValue("@p_balconies3", txtBalconies3.Text);
        cmd.Parameters.AddWithValue("@p_balcony_area3", "0");
        cmd.Parameters.AddWithValue("@p_balconie_no3", "0");

        cmd.Parameters.AddWithValue("@p_maintance3", "0");
        cmd.Parameters.AddWithValue("@p_building3", "0");
        cmd.Parameters.AddWithValue("@p_boiler3", "0");


        cmd.Parameters.AddWithValue("@p_north", CheckBox9.Checked);
        cmd.Parameters.AddWithValue("@p_south", CheckBox10.Checked);

        cmd.Parameters.AddWithValue("@p_east", CheckBox11.Checked);
        cmd.Parameters.AddWithValue("@p_west", CheckBox12.Checked);

        cmd.Parameters.AddWithValue("@p_smart", CheckBox1.Checked);
        cmd.Parameters.AddWithValue("@p_alarm", CheckBox2.Checked);
        cmd.Parameters.AddWithValue("@p_bars", CheckBox3.Checked);
        cmd.Parameters.AddWithValue("@p_handi", CheckBox4.Checked);

        cmd.Parameters.AddWithValue("@p_teather", CheckBox5.Checked);
        cmd.Parameters.AddWithValue("@p_gym", CheckBox6.Checked);
        cmd.Parameters.AddWithValue("@p_zacuji", CheckBox7.Checked);


        cmd.Parameters.AddWithValue("@p_pool", CheckBox8.Checked);






        cmd.Parameters.AddWithValue("@status1", "0");
        cmd.Parameters.AddWithValue("@adding_date", System.DateTime.Now);
        cmd.Parameters.AddWithValue("@adding_by", Session["email"]);


        cmd.ExecuteNonQuery();



        if (FileUpload2.HasFile)
        {

            string fileName = Path.GetFileName(FileUpload2.PostedFile.FileName);

            string fileExtension = Path.GetExtension(FileUpload2.PostedFile.FileName);

            string documentType = string.Empty;



            //provide document type based on it's extension

            switch (fileExtension)
            {

                case ".pdf":

                    documentType = "application/pdf";

                    break;

                case ".xls":

                    documentType = "application/vnd.ms-excel";

                    break;

                case ".xlsx":

                    documentType = "application/vnd.ms-excel";

                    break;

                case ".doc":

                    documentType = "application/vnd.ms-word";

                    break;

                case ".docx":

                    documentType = "application/vnd.ms-word";

                    break;

                case ".gif":

                    documentType = "image/gif";

                    break;

                case ".png":

                    documentType = "image/png";

                    break;

                case ".jpg":

                    documentType = "image/jpg";

                    break;

            }



            //Calculate size of file to be uploaded

            int fileSize = FileUpload2.PostedFile.ContentLength;



            //Create array and read the file into it

            byte[] documentBinary = new byte[fileSize];

            FileUpload2.PostedFile.InputStream.Read(documentBinary, 0, fileSize);
            SqlConnection conn2 = null;
            conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            conn2.Open();
            //  SqlCommand cmd1 = new SqlCommand("insert into task values(@action_,@c_id ,@added_by, @c_name,@to_do ,@created ,@updated,@agent,@sec_agent,@DocName,@Type_,@DocData,@rank_,@commission,@refer,@comment)", conn2);
            SqlCommand cmd1 = new SqlCommand("insert into task values(@action_,@c_id ,@added_by, @c_name,@to_do ,@created ,@updated,@fast_reminder,@agent,@sec_agent,@rank_,@commission,@refer,@comment,@DocName,@Type_,@DocData)", conn2);

            cmd1.Parameters.AddWithValue("@action_", "0");
            cmd1.Parameters.AddWithValue("@c_id", DropDownList1.SelectedValue);
            cmd1.Parameters.AddWithValue("@added_by", Session["email"]);

            cmd1.Parameters.AddWithValue("@c_name", DropDownList3.SelectedValue);
            cmd1.Parameters.AddWithValue("@to_do", DropDownList4.SelectedValue);
            cmd1.Parameters.AddWithValue("@created", txtcreate.Text);
            cmd1.Parameters.AddWithValue("@updated", txtupdate.Text);
            cmd1.Parameters.AddWithValue("@fast_reminder", txtreminder.Text);
            cmd1.Parameters.AddWithValue("@agent", txtagent.Text);
            cmd1.Parameters.AddWithValue("@sec_agent", DropDownList6.SelectedValue);
            cmd1.Parameters.AddWithValue("@rank_", txtrank.Text);
            cmd1.Parameters.AddWithValue("@commission", txtagent.Text);
            cmd1.Parameters.AddWithValue("@refer", TextBox2.Text);
            cmd1.Parameters.AddWithValue("@comment", txtcomment.Text);
            SqlParameter DocName = new SqlParameter("@DocName", SqlDbType.VarChar, 50);

            DocName.Value = fileName.ToString();

            cmd1.Parameters.Add(DocName);
            SqlParameter Type = new SqlParameter("@Type_", SqlDbType.VarChar, 50);

            Type.Value = documentType.ToString();

            cmd1.Parameters.Add(Type);



            SqlParameter uploadedDocument = new SqlParameter("@DocData", SqlDbType.Binary, fileSize);

            uploadedDocument.Value = documentBinary;

            cmd1.Parameters.Add(uploadedDocument);



            cmd1.ExecuteNonQuery();











            Response.Redirect("partner_property_add_confirmation.aspx");
    


        }
    }

    protected void LinkButton1_Click(object sender, System.EventArgs e)
    {
        Session.Abandon();
          Response.Redirect("../partner.aspx");
    }

}